import { loginUrl, bindWxUrl, serveUrl } from '../../api/index'
import { userStatusUrl } from '../../api/individual'
import { post, individualEntry, getQueryString, previewDocument } from '../../utils/index'
import WxValidate from '../../utils/WxValidate'
Page({
  data: {
    isRead: false,
    isFirstLogin: true,
    isLogin: true,
    form: {
      tenantId: '000000',
      username: '',
      password: '',
      type: 'account',
      grant_type: 'password',
      scope: 'all'
    },
    userType: 'individual',
    formError: {}
  },
  async login () { // 登录操作
    const { form, userType } = this.data
    if (!this.WxValidate.checkForm(form)) {
      const formError = this.WxValidate.formError
      this.setData({
        formError
      })
      return
    }
    this.setData({ formError: {} })
    const { tenantId } = form
    const userInfo = await post(loginUrl, form, { 'Tenant-Id': tenantId, 'User-Type': userType }, false)
    this.saveInfo(userInfo)
    this.bandWx()
  },
  bandWx () { // 帐号微信绑定
    wx.login({
      success: async res => {
        const { code } = res
        const { form: { tenantId }, userType } = this.data
        await post(bindWxUrl, { code, 'systemType': 'dispatch' }, { 'Tenant-Id': tenantId, 'User-Type': userType }, false)
      }
    })
  },
  async saveInfo (userInfo) { // 保存用户信息，并做跳转操作
    const { userType } = this.data
    wx.setStorageSync('user', userInfo)
    wx.setStorageSync('token', userInfo.access_token)
    // if (userType === 'individual') {
    //   const { data } = await post(userStatusUrl)
    //   individualEntry(data)
    //   return
    // }
    wx.navigateTo({ url: '/pages/hr/hr' })
  },
  initValidator () { // 初始化验证器
    const rules = {
      username: { required: true, tel: true },
      password: { required: true, maxlength: 32 }
    }
    const messages = {
      username: { required: '请输入手机号码', tel: '请填写正确手机号' },
      password: { required: '请输入密码', maxlength: '密码长度不大于32位' }
    }
    this.WxValidate = new WxValidate(rules, messages)
  },
  register () { // 注册
    wx.navigateTo({ url: '/pages/register/register' })
  },
  changeTab (e) { // 切换tab
    const { tab } = e.target.dataset
    this.setData({
      userType: tab
    })
  },
  usernameChange (e) { // 账号
    this.data.form.username = e.detail
  },
  pwdChange (e) { // 密码
    this.data.form.password = e.detail
  },
  getQuery (q) { // 扫码后获取参数
    const url = decodeURIComponent(q)
    const companyId = getQueryString(url, 'companyId')
    const confirmId = getQueryString(url, 'confirmId')
    wx.setStorageSync('companyId', companyId)
    wx.setStorageSync('confirmId', confirmId)
  },
  toRead () { // 微信登录授权页
    wx.navigateTo({ url: '/pages/read/read' })
  },
  async init () {
    // const user = wx.getStorageSync('user')
    // if (!user) {
      this.initValidator()
      this.setData({ isLogin: false })
      return
    // }
    // if (user.role_name === 'individual') {
    //   try {
    //     const { data } = await post(userStatusUrl)
    //     individualEntry(data)
    //     if (data.status > 3) {
    //       this.initValidator()
    //       this.setData({ isLogin: false })
    //     }
    //   } catch (e) {
    //     console.log(e)
    //     this.initValidator()
    //     this.setData({ isLogin: false })
    //   }
    //   return
    // }
    // wx.navigateTo({ url: '/pages/enterprise/home/home' })
  },
  onChange ({ detail }) { // 同意条款
    this.setData({ isRead: detail })
  },
  handleRead () { // 阅读条款
    previewDocument(serveUrl)
  },
  onShow () {
    if (!this.data.isFirstLogin) {
      this.initValidator()
      this.setData({ isLogin: false })
    } else {
      this.setData({ isFirstLogin: false })
    }
  },
  onLoad ({ q }) {
    if (q) {
      this.getQuery(q)
    }
    this.init()
  }
})